/*
Copyright Tophy 11/5/2007
class action of scroll2
1.scroll hand on bar from left to right or top to bottom
2.change scroll zone by change hand's left-part or right-part
3.scroll thrigh click blank zone
*/

dynamic class Scroll2{
	public static var SM_SCROLL="scroll";
	public static var SM_CHANGETIMEZONE="changeTimeZone";
	
	private var $vorh:Boolean;
	private var $hand:MovieClip;
	private var $bar:MovieClip;
	private var $leftb:MovieClip;
	private var $rightb:MovieClip;
	private var $probt:MovieClip;
	private var $nextbt:MovieClip;
	private var $moving:Boolean;
	
	private var $min:Number;
	private var $max:Number;
	
	private var $valueMin:Number;
	private var $valueMax:Number;
	private var $value:Number;
	
	private var $mouseObj:Object;
	
	private var say:Say;
	//----------------------------------- a
	
	public function get Value():Number{
		return this.$value;
	}
	public function set Value(value:Number){
			this.$value=value;
			/*
		if(value<this.$min){
			this.Value=this.$min;
		}else if(value>this.$max){
			this.Value=this.$max;
		}else{
			this.$value=value;
		}
		*/
		this.setValues(value);
	}
	public function get ValueMin():Number{
		return this.$valueMin;
	}
	public function set ValueMin(value:Number){
		if(value>this.$valueMax){
			value=this.$valueMax;
		}
		this.$valueMin=value;
	}
	
	public function get ValueMax():Number{
		return this.$valueMax;
	}
	public function set ValueMax(value:Number){
		if(value<this.$valueMin){
			value=this.$valueMin;
		}
		this.$valueMax=value;
	}
	
	
	public function get Min():Number{
		return this.$min;
	}
	public function set Min(value:Number){
		
		this.$min=value;
		if(this.$value<value){
			this.Value=value;
		}
		if(this.$valueMax<value){
			this.ValueMax=value;
			this.ValueMin=value;
		}else if(this.$valueMin<value && this.$valueMax>=value){
			this.$valueMin=value;
		}
	}
	
	public function get Max():Number{
		return this.$max;
	}
	public function set Max(value:Number){
		
		this.$max=value;
		if(this.$value>value){
			this.Value=value;
		}
	}
	
	public function get Name():String{
		return this.say.Name;
	}
	public function set Name(value:String){
		this.say.Name=value;
	}
	////////////////////////////////////////////////////////////
	
	/**
	 * Scroll2
	 * @param	vorh
	 * @param	Hand
	 * @param	Bar
	 * @param	leftbor
	 * @param	rightbor
	 * @param	probt
	 * @param	nextbt
	 */
	public function Scroll2(vorh:Boolean,Hand:MovieClip,Bar:MovieClip,leftb:MovieClip,rightb:MovieClip,probt:MovieClip,nextbt:MovieClip){
		vorh=((vorh==undefined)? true:vorh);
		this.$vorh=vorh;
		this.say=new Say("scroll2");
		this.$hand=Hand;
		this.$bar=Bar;
		this.$leftb=leftb;
		this.$rightb=rightb;
		this.$probt=probt;
		this.$nextbt=nextbt;
		initvars();
		this.initBroadCase();
		this.say.msay("init ok! - "+this.toString());
	}
	private function initvars(){
		this.$mouseObj=new Object();
		this.$mouseObj.onMouseMove=this.MouseMoveFunMNT;
		this.$mouseObj["root"]=this;
		this.$mouseObj["mouseX"]=0;
		this.$mouseObj["mouseY"]=0;
		this.$moving=false;
		
		
		this.$min=0;
		this.$max=100;
		this.$valueMin=0;
		this.$valueMax=20;
	}
	public function init():Boolean{
		if(this.$bar==undefined || this.$hand==undefined){
			this.say.esay("Hand or Bar not set!");
			return false;
		}
		//trace("step0")
		this.initHandAndBarEvents();
		
		
		//trace("step1")
		Mouse.removeListener(this.$mouseObj);
		//trace("step2")
		Mouse.addListener(this.$mouseObj);
		//trace("step3")
	}
	private function initBroadCase(){
		AsBroadcaster.initialize(this);
	}
	private function initHandAndBarEvents(){
		this.$hand["root"]=this;
		this.$bar["root"]=this;
		this.$hand._x=this.$bar._x;
		this.$hand._y=this.$bar._y;
		this.$hand.onPress=(this.$vorh? this.HandMouseDownHFun:this.HandMouseDownVFun);
		this.$hand.onRelease=this.$hand.onReleaseOutside=(this.$vorh? this.HandMouseUpHfun:this.HandMouseUpVfun);
		//trace(this.$hand.onMouseDown)
	}
	
	private function MouseMoveFunMNT(){
		var root:Scroll2=this["root"];
		if(root==undefined || root.$bar==undefined || root.$hand==undefined) {
			Mouse.removeListener(this);
			return;
		}
		if(_root._xmouse==this["mouseX"] && _root._ymouse==this["mouseY"]){
			return;
		}
		if(!root.$moving) return;
		root.setValues();
		if(root.$vorh){
			if(_root._xmouse==this["mouseX"]){
				return;
			}
			//h
			//root.say.msay("value("+root.Value+")=["+root.ValueMin+"-"+root.ValueMax+"]");
			//root.say.msay("h "+root.$hand._x);
			root.broadcastMessage(Scroll2.SM_SCROLL,root);
		}else{
			if(_root._ymouse==this["mouseY"]){
				return;
			}
			root.broadcastMessage(Scroll2.SM_SCROLL,root);
			//v
			//root.say.msay("value("+root.Value+")=["+root.ValueMin+"-"+root.ValueMax+"]");
			//root.say.msay("h "+root.$hand._y);
		}
		
	}
	
	private function setValues(value:Number){
		if(value==undefined){
			if(this.$vorh){
				var ttl:Number=this.$bar._width-this.$hand._width;
				var nl:Number=this.$hand._x-this.$bar._x;
				var bl:Number=this.round(nl/ttl,2);
				var ttv:Number=this.$max-this.$min;
				
				//value
				this.$value=this.$min+ttv*bl;
				//trace(ttl+"|"+nl+"|"+bl+"|"+ttv+" == "+this.$value);
				
				ttl=this.$bar._width;
				nl=this.$hand._x-this.$bar._x;
				bl=this.round(nl/ttl,2);
				nl+=this.$hand._width;
				var bl2=this.round(nl/ttl,2);
				//valueMin & valueMax
				this.$valueMin=this.$min+ttv*bl;
				this.$valueMax=this.$min+ttv*bl2;
				
			}else{
				var ttl:Number=this.$bar._height-this.$hand._height;
				var nl:Number=this.$hand._y-this.$bar._y;
				var bl:Number=this.round(nl/ttl,2);
				var ttv:Number=this.$max-this.$min;
				//value
				this.$value=this.$min+ttv*bl;
				
				ttl=this.$bar._height;
				nl=this.$hand._y-this.$bar._y;
				bl=this.round(nl/ttl,2);
				nl+=this.$hand._height;
				var bl2=this.round(nl/ttl,2);
				//valueMin & valueMax
				this.$valueMin=this.$min+ttv*bl;
				this.$valueMax=this.$min+ttv*bl2;
				
			}
		}else{
			var ll:Number = this.$max - this.$min;
			var bl:Number = value / ll;
			var ttl:Number = this.$bar._width - this.$hand._width;
			this.$hand._x = ttl * bl; +this.$bar._x;
			this.setValues();
		}
		
	}
	private function round(n1:Number, n2:Number) {
		if (n2 == undefined) {
			n2 = 8;
		}
		var n:Number = Math.pow(10, n2);
		n1 *= n;
		n1 = Math.round(n1);
		n1 /= n;
		return n1;
	}

	private function toString(){
		return this.say.Name+"["+this.$min+"-"+this.$max+"]";
	}
	private function HandMouseUpVfun(){
		var root:Scroll2=this.root;
		this.stopDrag();
		root.$moving=false;
	}
	private function HandMouseUpHfun(){
		var root:Scroll2=this.root;
		this.stopDrag();
		root.$moving=false;
	}
	private function HandMouseDownVFun(){
		var root:Scroll2=this.root;
		this.startDrag(false,root.$bar._x,root.$bar._y,root.$bar._x,root.$bar._y+root.$bar._height-root.$hand._height);
		root.$moving=true;
	}
	
	private function HandMouseDownHFun(){
		var root:Scroll2=this.root;
		this.startDrag(false,root.$bar._x,root.$bar._y,root.$bar._x+root.$bar._width-root.$hand._width,root.$bar._y);
		root.$moving=true;
	}
	
}